package com.jss.jssmybatisplus.dto;

import com.alibaba.fastjson.JSONObject;
import lombok.Data;

import java.util.UUID;

/**
 * @Author Shuaishuai.Ji
 * @Date 2022/8/23 10:34
 * @Version 1.0
 */
@Data
public class ResultResponse<T> {
    private static final String SUCCESS_CODE = "0";

    private static final String SUCCESS_MSG = "success";

    private String requestId;

    private String code;

    private String message;
    private T data;

    public ResultResponse(){
        this.requestId = UUID.randomUUID().toString();
    }

    public ResultResponse(String reqId){
        this.requestId = reqId;
    }

    public ResultResponse(String reqId, T payload) {
        this.requestId = reqId;
        this.code = SUCCESS_CODE;
        this.data = payload;
    }

    public ResultResponse(String reqId, String code, String msg) {
        this.requestId = reqId;
        this.code = code;
        this.message = msg;
    }

    public ResultResponse(String reqId, String code, String msg, T data) {
        requestId = reqId;
        this.code = code;
        this.message = msg;
        this.data = data;
    }

    public static <T> ResultResponse<T> succResult(){
        ResultResponse<T> resultResponse = new ResultResponse<>();
        resultResponse.setCode(SUCCESS_CODE);
        resultResponse.setMessage(SUCCESS_MSG);
        return resultResponse;
    }

    public static <T> ResultResponse<T> succResult(T data) {
        ResultResponse<T> resultDTO = new ResultResponse<>();
        resultDTO.setCode(SUCCESS_CODE);
        resultDTO.setMessage(SUCCESS_MSG);
        resultDTO.setData(data);
        return resultDTO;
    }

    public static <T> ResultResponse<T> succResult(String reqId, T data){
        ResultResponse<T> resultResponse = new ResultResponse<>();
        resultResponse.setRequestId(reqId);
        resultResponse.setCode(SUCCESS_CODE);
        resultResponse.setMessage(SUCCESS_MSG);
        resultResponse.setData(data);
        return resultResponse;
    }


    public static <T> ResultResponse<T> errorResult(String code, String message) {
        ResultResponse<T> resultDTO = new ResultResponse<>();
        resultDTO.setCode(code);
        resultDTO.setMessage(message);
        return resultDTO;
    }

//    public static <T> ResultResponse<T> errorResult(IResultCode resultCode) {
//        ResultResponse<T> resultDTO = new ResultResponse<>();
//        resultDTO.setCode(resultCode.getErrorCode());
//        resultDTO.setMessage(resultCode.getErrorMessage());
//        return resultDTO;
//    }

    public static <T> ResultResponse<T> errorResult(String reqId, String code, String message){
        ResultResponse<T> resultResponse = new ResultResponse<>();
        resultResponse.setRequestId(reqId);
        resultResponse.setCode(code);
        resultResponse.setMessage(message);
        return resultResponse;
    }

    public static <T> ResultResponse<T> errorResult(String reqId, String code, String message, T data){
        ResultResponse<T> resultResponse = new ResultResponse<>();
        resultResponse.setRequestId(reqId);
        resultResponse.setCode(code);
        resultResponse.setMessage(message);
        resultResponse.setData(data);
        return resultResponse;
    }

    public static <T> ResultResponse<T> errorResult(String code, String message, T data){
        ResultResponse<T> resultResponse = new ResultResponse<>();
        resultResponse.setCode(code);
        resultResponse.setMessage(message);
        resultResponse.setData(data);
        return resultResponse;
    }

    @Override
    public String toString() {
        return JSONObject.toJSONString(this);
    }
}
